package vworld.dao.user;

import generated.domain.avatar.Avatar;
import generated.domain.user.User;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import vworld.dao.HibernateUtil;
import vworld.dao.avatar.AvatarDAO;

public class UserDAO {

	public boolean createUser(User pUser) {
		Transaction tx = null;
		Session session = HibernateUtil.currentSession();
		try {
			tx = session.beginTransaction();
			session.save(pUser);
			tx.commit();
			return true;
		} catch (HibernateException e) {
			e.printStackTrace();
			if (tx != null && tx.isActive())
				tx.rollback();
		} finally {
			HibernateUtil.closeSession();
		}
		return false;
	}

	public void deleteUser(User pUser) {
		Transaction tx = null;
		Session session = HibernateUtil.currentSession();
		try {
			tx = session.beginTransaction();
			session.delete(pUser);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			if (tx != null && tx.isActive())
				tx.rollback();
		} finally {
			HibernateUtil.closeSession();
		}
	}

	public void saveOrUpdateUser(User pUser) {
		Transaction tx = null;
		Session session = HibernateUtil.currentSession();
		try {
			tx = session.beginTransaction();
			if (pUser.getAvatar() != null) {
				session.saveOrUpdate(pUser.getAvatar());
			}
			session.saveOrUpdate(pUser);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			if (tx != null && tx.isActive())
				tx.rollback();
		} finally {
			HibernateUtil.closeSession();
		}
	}

	public User getUser(String pUserLogin) {
		try {
			Session session = HibernateUtil.currentSession();
			Query query = session
					.createQuery("from User u where u.login = :login");
			query.setString("login", pUserLogin);

			for (Object obj : query.list()) {
				return (User) obj;
			}
			return null;
		} finally {
			HibernateUtil.closeSession();
		}
	}

	public User getUser(String login, String password) {
		try {
			Session session = HibernateUtil.currentSession();
			Query query = session
					.createQuery("from User u where u.login = :login and u.password = :password");
			query.setString("login", login);
			query.setString("password", password);

			for (Object obj : query.list()) {
				return (User) obj;
			}
			return null;
		} finally {
			HibernateUtil.closeSession();
		}
	}

	public Avatar getAvatar(User pUser) {
		try {
			Session session = HibernateUtil.currentSession();
			Query query = session
					.createQuery("select avatar from User u where u.login = :login");
			query.setString("login", pUser.getLogin());
			for (Object obj : query.list()) {
				return (Avatar) obj;
			}
			return null;
		} finally {
			HibernateUtil.closeSession();
		}
	}

	public boolean removeAvatar(User pUser) {
		AvatarDAO avatarDAO = new AvatarDAO();
		avatarDAO.deleteAvatar(pUser.getAvatar());
		return true;
	}
}
